package com.cherry.androidviewdemo.widget

import android.content.Context
import android.graphics.*
import android.support.annotation.ColorInt
import android.util.AttributeSet
import android.view.View
import com.cherry.androidviewdemo.R
import com.cherry.androidviewdemo.utils.dp2px
import com.cherry.androidviewdemo.utils.sp2px

/**
 * @author DongMS
 * @since 2020/6/3
 */
class ProgressBarView @JvmOverloads constructor(context: Context, attr: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attr, defStyleAttr) {

    @ColorInt
    var outerColor = Color.GRAY

    @ColorInt
    var innerColor = Color.RED

    var borderWidth = 0

    var textSize = 0

    @ColorInt
    var textColor = Color.RED

    var progress = 0f
        set(value) {
            field = value
            invalidate()
        }

    var maxValue = 100f

    private val outerPaint: Paint

    private val innerPaint: Paint

    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)

    private val rectF = RectF()

    private val textRect = Rect()

    init {
        val array = context.obtainStyledAttributes(attr, R.styleable.ProgressBarView)
        outerColor = array.getColor(R.styleable.ProgressBarView_outerBackgroundColor, outerColor)
        innerColor = array.getColor(R.styleable.ProgressBarView_innerBackgroundColor, innerColor)
        borderWidth = array.getDimensionPixelSize(R.styleable.ProgressBarView_progressBorderWidth, 5f.dp2px(context).toInt())
        textSize = array.getDimensionPixelSize(R.styleable.ProgressBarView_progressTextSize, 12f.sp2px(context).toInt())
        textColor = array.getColor(R.styleable.ProgressBarView_progressTextColor, textColor)

        array.recycle()

        outerPaint = initPaint(outerColor)
        innerPaint = initPaint(innerColor)

        textPaint.color = textColor
        textPaint.textSize = textSize.toFloat()

    }

    private fun initPaint(backgroundColor: Int): Paint {
        return Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG).apply {
            color = backgroundColor
            style = Paint.Style.STROKE
            //setStrokeWidth这个方法，并不是往圆内侧增加圆环宽度的，而是往外侧增加一半，往内侧增加一半
            strokeWidth = borderWidth.toFloat()
            strokeCap = Paint.Cap.ROUND
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //确保形状是正方形
        val width = MeasureSpec.getSize(widthMeasureSpec) + paddingLeft + paddingRight
        val height = MeasureSpec.getSize(heightMeasureSpec) + paddingTop + paddingBottom
        val size = width.coerceAtLeast(height)
        setMeasuredDimension(size, size)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val center = width / 2f
        val radius = (width - borderWidth) / 2f

        //画外圆
        canvas.drawCircle(center, center, radius, outerPaint)

        //画内圆弧
        val percent = progress / maxValue
        val sweepAngle = percent * 360

        rectF.set(center - radius + paddingLeft, center - radius + paddingTop, center + radius - paddingRight, center + radius - paddingBottom)
        canvas.drawArc(rectF, 90f, sweepAngle, false, innerPaint)

        //画文字
        val progressText = "${(100 * percent).toInt()}%"
        textPaint.getTextBounds(progressText, 0, progressText.length, textRect)
        val dx = width / 2f - textRect.width() / 2f

        val fontMetrics = textPaint.fontMetrics
        val dy = (fontMetrics.bottom - fontMetrics.top) / 2f - fontMetrics.bottom
        val baseLine = height / 2f + dy
        canvas.drawText(progressText, dx, baseLine, textPaint)
    }


}